 /**
 * @file tal_data_receive.h
 * @brief Common process - data receive
 * @version 1.0.0
 * @date 2021-09-09
 *
 * @copyright Copyright 2018-2021 Tuya Inc. All Rights Reserved.
 *
 */
#ifndef __TAL_DATA_RECEIVE_H__
#define __TAL_DATA_RECEIVE_H__


#ifdef __cplusplus
    extern "C" {
#endif

#include "tuya_cloud_types.h"
#include "tuya_zigbee_stack.h"


/**
 * @enum the return status after receiving data is used for response
 */
typedef enum {
    ZCL_MSG_RET_SUCCESS = 0x00,         ///< successful
    ZCL_MSG_RET_FAILED  = 0x01,         ///< failed
    ZCL_MSG_RET_UNSUP_COMMAND = 0x81,   ///< support command
    ZCL_MSG_RET_UNSUP_ATTR    = 0x86,   ///< support attribute
    ZCL_MSG_RET_INVALID_VALUE = 0x87,   ///< invalid value
    ZCL_MSG_RET_UNSUP_CLUSTER = 0xC3,   ///< support cluster
} TAL_MSG_RET_E;

/**
 * @enum the return status after attribute is used for response
 */
typedef enum {
    WRITE_RET_SUCCESS = 0,              ///< successful
    WRITE_RET_UNSUP_ATTR = 0x86,        ///< support attribute
    WRITE_RET_INVALID_VALUE = 0x87,     ///< invalid value
} TAL_WRITE_RET_E;

/**
 * @enum zigbee zcl write attribute record format
 */
typedef struct {
	UINT16_T attr_id;                   ///< attribute id
	UINT8_T  data_type;                 ///< attribute data type
    UINT8_T  data_len;                  ///< atttribute data len
	UINT8_T  *attr_data;                ///< attribute data
} TAL_ATTR_REC_T;

/**
 * @enum zigbee zcl message
 */
typedef struct {
    ZG_DELIVERY_MODE_E mode;            ///< zigbee message delivery mode
    UINT8_T  direction;                 ///< data transmission direction
    UINT8_T  src_ep;                    ///< source endpoint
    UINT8_T  dst_ep;                    ///< destination endpoint
    UINT8_T  zcl_seq;                   ///< zcl sequence number
    UINT8_T  command;                   ///< zcl command id
    UINT8_T  length;                    ///< length of zcl message payload
    UINT16_T cluster;                   ///< cluster id
    UINT16_T src_addr;                  ///< source address
    UINT16_T group_id;                  ///< group id(0: indicating non multicast data)
    UINT8_T  *payload;                  ///< zcl message payload, not include zcl header
} TAL_ZCL_MSG_T;

/**
 * @enum zigbee zdo message
 */
typedef struct {
    ZG_DELIVERY_MODE_E mode;            ///< zigbee message delivery mode
    UINT8_T  src_ep;                    ///< source endpoint
    UINT8_T  dst_ep;                    ///< destination endpoint
    UINT8_T  length;                    ///< length of zdo packet
    UINT16_T cluster;                   ///< cluster id
    UINT16_T src_addr;                  ///< source address
    CONST UINT8_T *packet;              ///< zdo message packet
} TAL_ZDO_MSG_T;

/**
 * @brief pre-write attribute callback
 * @note < this is a weak function you can rewrite this API in app layer >
 * 
 * @param[in] ep_id:      endpoint id
 * @param[in] cluster_id: cluster id
 * @param[in] attr_rec:   point to attribute record 
 * @return write status
 */
TAL_WRITE_RET_E tal_zg_pre_write_attribute_callback(UINT8_T ep_id,
                                                    UINT16_T cluster_id,
                                                    TAL_ATTR_REC_T *attr_rec);

/**
 * @brief post-write attribute callback
 * @note < this is a weak function you can rewrite this API in app layer >
 * 
 * @param[in] ep_id:      endpoint id
 * @param[in] cluster_id: cluster id
 * @param[in] attr_rec:   point to attribute record 
 * @return write status
 */
TAL_WRITE_RET_E tal_zg_post_write_attribute_callback(UINT8_T ep_id, 
                                                    UINT16_T cluster_id,
                                                    TAL_ATTR_REC_T *attr_rec);

/**
 * @brief general message receive callback
 * @note < this is a weak function you can rewrite this API in app layer >
 *
 * @param[in] dev_msg:  point to receive message
 * @return message receive status
 */
TAL_MSG_RET_E tal_zcl_general_msg_recv_callback(TAL_ZCL_MSG_T *msg);

/**
 * @brief specific message receive callback
 * @note < this is a weak function you can rewrite this API in app layer >

 * @param[in] dev_msg:  point to receive message
 * @return message receive status
 */
TAL_MSG_RET_E tal_zcl_specific_msg_recv_callback(TAL_ZCL_MSG_T *msg);

/**
 * @brief whether to report the data immediately after receiving the broadcast
 * 
 * @param[in] none
 * @return FALSE: Delayed report, TRUE: report immediately
 */
BOOL_T tal_recv_bsct_report_immediately_flag(VOID_T);

#ifdef __cplusplus
}
#endif

#endif

